<template>
    <view :class="'main main-my-index main-' + skin">
        <!--top area begin-->
        <view :class="'user  bg-' + skin">
            <view class="school-cover" style="background-image: url(../../static/images/ic_social.png)"></view>
            <view class="user-bar">
                <view :class="'avatar round border-' + skin" v-if="userInfo.USER_PIC">
                    <image model="aspectFit" class="avatar xl round bg-gray" @tap="selectMyAvater" :src="userInfo.USER_PIC"></image>
                    <view class="tag badge bg-red icon-female" v-if="userInfo.sex == 2" style="z-index: 9999999"></view>
                    <view class="tag badge bg-blue icon-male" v-if="userInfo.sex == 1" style="z-index: 9999999"></view>
                </view>
                <view class="avatar xl round bg-gray" v-if="!userInfo.USER_PIC" style="flex: none">
                    <open-data class="avatar-open" type="userAvatarUrl"></open-data>
                </view>

                <view class="detail">
                    <view class="name text-cut">
                        {{ userInfo.name }}
                        <text style="font-weight: normal" class="text-s margin-left-xs">{{ userInfo.college }}</text>
                    </view>
                    <view class="desc">
                        <view class="tag small round bg-green">{{ userInfo.major }}</view>
                    </view>
                </view>
            </view>
        </view>
        <!--top area end-->

        <!--down area begin-->
        <view class="down">
            <view class="data shadow-warp">
                <view @tap="url" data-url="../../pages/bbs/bbs">
                    <text class="num">{{ userInfo.comment_num || 0 }}</text>
                    <text class="txt">我的留言</text>
                </view>
                <view @tap="url" data-url="../../pages/mymaster/mymaster">
                    <text class="num">{{ userInfo.master_num || 0 }}</text>
                    <text class="txt">拾得物品</text>
                </view>
                <view @tap="url" data-url="../../pages/mything/mything">
                    <text class="num">{{ userInfo.thing_num || 0 }}</text>
                    <text class="txt">丢失物品</text>
                </view>
				
				
                
            </view>

            <!--base begin -->
            <view class="comm-list menu card-menu margin-top shadow radius">
                <view class="item arrow" @tap="url" data-url="/pages/user/edit/edit">
                    <view class="content">
                        <text class="icon-myfill item-icon text-olive"></text>
                        <text class="text-black">修改个人资料</text>
                    </view>
                </view>
            </view>
            <!--base end -->

            <!--act begin -->
            <view class="comm-list menu card-menu margin-top shadow radius">
                <view class="item arrow" @tap="url" data-url="/pages/bbs/bbs">
                    <view class="content">
                        <text class="icon-comment item-icon text-red"></text>
                        <text class="text-black">我的留言</text>
                    </view>
                </view>
                <view class="item arrow" v-if="WebConfig.open_comment === '1'" @tap="url" data-url="../mything/mything">
                    <view class="content">
                        <text class="icon-search item-icon text-orange"></text>
                        <text class="text-black">我发布的寻物启事</text>
                    </view>
                </view>


                <view class="item arrow" v-if="WebConfig.open_comment === '1'" @tap="url" data-url="../mymaster/mymaster">
                    <view class="content">
                        <text class="icon-new item-icon text-yellow"></text>
                        <text class="text-black">我发布的失物信息（寻找物主）</text>
                    </view>
                </view>
            </view>
            <!--act end -->

            <!--sys begin -->
            <view class="comm-list menu card-menu margin-top shadow radius">
				
                <button class="item arrow" open-type="feedback" style="width: auto; height: 100rpx; display: flex; box-sizing: border-box; left: 0rpx; top: 0rpx">
                    <view class="content text-left">
                        <text class="icon-pick item-icon text-bold"></text>
                        <text class="text-black">bug提交</text>
                    </view>
                </button>
				
                <view class="item arrow" @tap="url" data-url="/pages/about/about">
                    <view class="content">
                        <text class="icon-infofill item-icon text-green"></text>
                        <text class="text-black">关于我们</text>
                    </view>
                </view>
            </view>
			<view class="comm-list menu card-menu margin-bottom-xl shadow radius">
			    <view class="item arrow" @tap="quit">
			        <view class="content">
			            <text class="icon-exit item-icon text-blue"></text>
			            <text class="text-black">退出登录</text>
			        </view>
			    </view>
			</view>
			
            <!--sys end -->
        </view>
        <!--down area end-->
		
        <view class="site-footer">
            <view class="link">
                <view @tap="url" data-url="/pages/about/about">关于我们</view>
            </view>
            <view class="info">Copyright © 一个小团队 版权所有</view>
        </view>
    </view>
</template>

<script>
const app = getApp();

const getData = (url, param) => {
    return new Promise((resolve, reject) => {
        uni.request({
            url: url,
            method: 'POST',
            data: param,
            header: {
                'Content-Type': 'application/json'
            },

            success(res) {
                resolve(res.data);
            },

            fail(err) {
                reject(err);
            }
        });
    });
};

export default {
    data() {
        return {
            openid: '',

            userInfo: {
                USER_PIC: '',
                sex: 0,
                major: '',
                department: '',
                comment_num: '',
                help_num: '',
                thing_num: '',
                pet_num: ''
            },

            skin: 'skin1',

            WebConfig: {
                open_comment: ''
            }
        };
    }
    /**
     * 生命周期函数--监听页面加载
     */,
    onLoad: function (options) {
        uni.showLoading({
            title: '加载中...'
        });
        var that = this;
        getData(app.globalData.ApiUrl + 'api/Wechat/WebConfig').then((msg) => {
            that.setData({
                WebConfig: msg
            });
            uni.hideLoading();
        });
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {
        this.SafeCode = uni.getStorageSync('SafeCode');
        var that = this;
        uni.request({
            url: app.globalData.ApiUrl + 'api/Android/UserInfo',
            //这里填写你的接口路径
            header: {
                'Content-Type': 'application/json'
            },
            data: {
                //这里写你要请求的参数
                SafeCode: this.SafeCode
            },
            success: function (res) {
                that.setData({
                    userInfo: res.data
                });
                console.log(that.userInfo);
            }
        });
    },
    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {
        this.refresh();
    },
    methods: {
		
		selectMyAvater:function(e){
			
			let that = this;
			
			uni.chooseImage({
				count:1,
				sizeType:['original'],
				success(res) {
					if(res.tempFilePaths.length===1){
						
						const imagePath = res.tempFilePaths[0];
						
						
						const uploadResult = uni.uploadFile({
							url:app.globalData.ApiUrl+"api/Android/uploadAvatar",
							filePath:imagePath,
							name:'avatar',
							formData:{
								'SafeCode':uni.getStorageSync('SafeCode')
							},
							success:(res)=>{
								
								
								let result = JSON.parse(res.data);
								
								if(result && result.result=="success"){
									
									
									uni.showToast({
										title:"上传成功",
										icon:"success",
										duration:2000
									})
									that.refresh();
								}
							},
							fail(res) {
								uni.showToast({
									title:"上传失败",
									icon:"error",
									duration:2000
								})
							}
							
						})
					}
				}
			})
		},
		
		
		refresh:function(){
			this.SafeCode = uni.getStorageSync('SafeCode');
			var that = this;
			uni.request({
			    url: app.globalData.ApiUrl + 'api/Android/UserInfo',
			    //这里填写你的接口路径
			    header: {
			        'Content-Type': 'application/json'
			    },
			    data: {
			        //这里写你要请求的参数
			        SafeCode: that.SafeCode
			    },
			    success: function (res) {
			        that.setData({
			            userInfo: res.data
			        });
			        console.log(that.SafeCode);
			    }
			});
			
			uni.stopPullDownRefresh();
		},
		
        url: function (e) {
            uni.navigateTo({
                url: e.currentTarget.dataset.url
            });
        },
		quit:function(e){
			uni.removeStorage({
				key: 'SafeCode',
				success:function(){
					uni.showModal({
						title:"退出",
						content:"退出成功！",
						success:(res)=>{
							uni.navigateTo({
								url: '../../pages/bind/bind'
							});
						},
						error:function(){
							uni.navigateTo({
								url: '../../pages/bind/bind'
							});
							
						}
					})
					
				}
			});
		}
    }
};
</script>
<style>
@import './user.css';
</style>
